package com.lijie.ljblog.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.PageInfo;
import com.lijie.ljblog.model.Liuyan;
import com.lijie.ljblog.service.LiuyanService;
import com.lijie.ljblog.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lijie
 * @since 2021-08-28
 */
@RestController
@RequestMapping("ljblog/liuyan/")
public class LiuyanController {
    @Autowired
    LiuyanService liuyanService;
    @RequestMapping("getLiuyanList/{page}")
    public Result<PageInfo<Liuyan>> getLiuyanList(@PathVariable Integer page, @RequestBody Liuyan liuyan){
        Result<PageInfo<Liuyan>> result=new Result<>(Result.STATUS_CODE_500);
        if (liuyan.getBlogid()!=null){
            PageInfo<Liuyan> liuyanList = liuyanService.getLiuyanList(page, liuyan);
            result.setData(liuyanList);
            result.setStatus(Result.STATUS_CODE_SUCCESS);
        } else {
            result.setMessage("博客Id为空");
        }
        return result;
    }
    @RequestMapping("commitLiuyan")
    public Result<Object> commitLiuyan(@RequestBody Liuyan liuyan, HttpServletRequest request){
        Result<Object> result=new Result<>(Result.STATUS_CODE_500);
        if(liuyan.getPersonId()!=null&&liuyan.getBlogid()!=null&&liuyan.getMessage()!=null){
            Boolean aBoolean = liuyanService.commitLiuyan(liuyan,request);
            if(aBoolean){
                result.setStatus(Result.STATUS_CODE_SUCCESS);
                result.setMessage("留言成功");
            }else{
                result.setMessage("留言失败");
            }
        }else{
            result.setMessage("留言人或留言博客或留言内容为空");
        }
        return result;
    }
}

